<?php
/**
 * @author GhostWei
 * @date Aug 22, 2012
 */
class controller_admin_picture_ajax extends system_user_controller_ajax {
	
	public function init() {
		if(!parent::init()) {
			$this->_error('Login time out or other reason.');
		}
	}
	
	private function _sepcialList($where = null) {
		$page = intval($this->_post('page'));
		if(!($lang = $this->_post('lang'))) {
			$lang = $this->user->getLang();
		}

		$adapter = new system_paginator_adapter_concept(new picture_special_concept($lang));
		if($where !== null) {
			$adapter->where($where);
		}
		if(1 && $category_id = intval($this->_post('category_id'))) {
			$adapter->where(array('special.id IN(SELECT `special_id` FROM `picture_special_category` WHERE `category_id` = ?)' => $category_id));
		}
		$keyword = trim($this->_post('keyword'));
		if($keyword == self::$_keyword) $keyword = '';
		if($keyword) {
			$keyword = addslashes($keyword);
			$in = "SELECT `special_id` FROM `picture_special_locale` WHERE `title` LIKE '%$keyword%' AND `lang` = '$lang'";
			$adapter->where("`name` LIKE '%$keyword%' OR `id` IN($in)");
		}
		$adapter->setCurrentPageNumber($page);
		$adapter->setLimit(20);
		$adapter->order('special.id DESC');
		$list = new system_paginator($adapter);
		
		$this->_data->html = $this->view->partial("picture/ajax/specialList.phtml", array('list' => $list, 'page' => $page));
		$this->_output();
	}
	
	public function action_importSpecial() {
		$lang = $this->user->getLang();
		$this->_sepcialList("special.id NOT IN(SELECT `special_id` FROM `picture_special_locale` WHERE `lang` = '$lang')");
	}
	
	public function action_addSpecialToTopic() {
		$this->_sepcialList();
	}
	
	public function action_addSpecialToCategory() {
		$this->_sepcialList();
	}
	
	public function action_addPhotoToSpecial() {
		$this->_sepcialList();
	}
	
	public function action_specialAddPhoto() {
		$page = intval($this->_post('page'));

		$adapter = new system_paginator_adapter_concept(new picture_photo_concept($this->user->getLang()));
		$adapter->setCurrentPageNumber($page);
		$adapter->setLimit(36);
		$adapter->order('photo.id DESC');
		$list = new system_paginator($adapter);
		
		$this->_data->html = $this->view->partial('picture/ajax/photoList.phtml', array('list' => $list, 'page' => $page));
		$this->_output();
	}	
	
}
